'use strict';
var nunjcks=require('nunjucks');

function createEnv(path,opts){
    path=path || 'views';
    opts=opts || {};
    var envOptions={
        autoescape: opts.autoescape === undefined ? true : opts.autoescape,
        throwOnUndefined: opts.throwOnUndefined === undefined ? false : opts.throwOnUndefined,
        trimBlocks: opts.trimBlocks === undefined ? false : opts.trimBlocks,
        lstripBlocks: opts.lstripBlocks === undefined ? false : opts.lstripBlocks,
        watch: opts.watch === undefined ? true : opts.watch,
        noCache: opts.noCache === undefined ? true : opts.noCache
    }
    var env=nunjcks.configure(path,envOptions);
    return env;
}
module.exports=async(ctx,next)=>{
    ctx.render=function(view,model){
        var env=createEnv('views',{autoescape:false});
        ctx.body=env.render(view,model)
    }
    await next();
}